<template>
  <div id="loginPage">
    <div class="login-bg" />
    <div class="login-info">
      <!--      <div class="login-logo" />-->
      <div class="login-form">
        <div class="login-title">
          <div>安徽省经济预测模型</div>
          <p>Anhui Province Economic Forecasting Model</p>
        </div>

        <el-form ref="accountForm" :model="accountForm" :rules="rules" label-position="left">
          <el-form-item prop="phone" label="">
            <div class="input-box">
              <img :src="require('@/assets/images/login/user.png')" alt="">
              <el-input
                v-model="accountForm.phone"
                type="text"
                size="large"
                maxlength="11"
                placeholder="请输入账号"
                autocomplete="off"
                @keydown.enter.native="handleLogin('accountForm')"
              />
            </div>
          </el-form-item>
          <el-form-item prop="password" label="">
            <div class="input-box pr30">
              <img :src="require('@/assets/images/login/password.png')" alt="">
              <el-input
                v-model="accountForm.password"
                maxlength="20"
                :type="pw ? 'password' : 'text'"
                placeholder="请输入登录密码"
                autocomplete="off"
                @keydown.enter.native="handleLogin('accountForm')"
              />
              <i class="el-icon-view cp" @click="pw=!pw" />
            </div>
          </el-form-item>
          <el-form-item>
            <el-button class="login-bt fs24" @click="handleLogin('accountForm')">
              登 录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { setToken } from '@/utils/auth'

import { validatePhone } from '@/utils/validate'
import { accountLoginListData } from './data'
export default {
  name: 'Login',
  data() {
    const validatePhoneNumber = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入账号'))
      } else if (!validatePhone(value)) {
        callback(new Error('请输入正确的账号'))
      } else {
        callback()
      }
    }
    return {
      pw: true,
      accountForm: {
        phone: '',
        password: ''
      },
      rules: {
        phone: [{ required: true, trigger: 'blur', validator: validatePhoneNumber }],
        password: [{ required: true, message: '请输入登录密码', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    window.sessionStorage.clear()
  },
  methods: {
    handleLogin(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 账号登录
          const accountMaps = accountLoginListData.map(item => { return item.phone })

          if (accountMaps.includes(this.accountForm.phone)) {
            const index = accountMaps.indexOf(this.accountForm.phone)
            if (accountLoginListData[index].password === this.accountForm.password) {
              setToken('is_login')
              this.$router.push('/home/index')
            } else {
              this.$message.error('账号或者密码错误，请重新输入！')
            }
          } else {
            this.$message.error('账号或者密码错误，请重新输入！')
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
#loginPage {
  min-height: 100vh;
  position: relative;
  overflow: hidden;
}

.login-bg {
  height: 100vh;
  background: url("~@/assets/images/bg.png") no-repeat right top;
  background-size: cover;
}

.login-info {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  margin: auto;
  height: fit-content;
  width:600px;
  .login-form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 46px 53px;
    background: rgba(255, 255, 255, .2);
    box-shadow: 4px 10px 20px -2px rgba(0, 0, 0, 0.2), 0 -4px 20px -2px rgba(0, 0, 0, 0.2);
    border-radius: 12px;

    .el-form-item {
      margin-bottom: 30px;
    }

    .login-title {
      margin-bottom: 20px;
      img {
        display: inline-block;
        width: 135px;
        height: 133px;
      }

      font-weight: 700;
      font-size: 26px;
      color: #fff;
      line-height: 33px;
      p{
        font-size: 20px;
        font-weight: 400;
      }
    }

    .input-box {
      overflow: hidden;
      display: flex;
      align-items: center;
      padding-left: 15px;
      background: #FFFFFF;
      border-radius: 8px;
      border: 1px solid #CDCDCD;

      img {
        width: 20px;
        height: 20px;
      }

      i {
        font-size: 16px;
      }
    }

    .code-box {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }

    .login-bt {
      width: 100%;
      background: #0079FE;
      border-radius: 8px;
      font-size: 16px;
      font-weight: 400;
      color: #FFFFFF;
      border: 0;
    }

    .login-forget {
      text-align: right;
      font-size: 16px;
      font-weight: 400;
      color: #0079FE;
      line-height: 22px;
      margin-bottom: 20px;
      margin-top: -10px;

      span {
        cursor: pointer;
      }
    }

    .login-register {
      text-align: center;
      margin-top: -10px;
      font-size: 14px;
      font-weight: 400;
      color: #000000;
      line-height: 20px;

      span {
        cursor: pointer;
        color: #0079FE;
      }
    }
  }
}

/deep/ .el-input {
  input {
    flex: 1;
    background: #fff !important;
    border-radius: 8px;
    border: 0 !important;
  }

  input:-webkit-autofill {
    -webkit-box-shadow: 0 0 0 1000px #fff inset !important;
  }

  input::placeholder {
  }
}

.input-code {
  width: 122px;
  height: 40px;
  margin-left: 10px;
  img {
    width: 122px;
    height: 40px;
  }
}
</style>
